package simplescenegraph.spaceshooter;

import java.awt.Dimension;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

import simplescenegraph.GroupNode;
import simplescenegraph.LeafNode;
import simplescenegraph.PointLight;
import simplescenegraph.Renderer;
import simplescenegraph.RenderingCallback;
import simplescenegraph.SimpleSceneGraph;
import simplescenegraph.View;
import simplescenegraph.assets.models.obj.OBJModel;
import simplescenegraph.assets.models.obj.OBJModelLoader;
import simplescenegraph.math.Color4f;
import simplescenegraph.math.Vector3f;

public class SpaceShooterGame extends SimpleSceneGraph {
	
	private Spaceship spaceship;
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("Space Shooter Game");
		frame.setPreferredSize(new Dimension(640, 480));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		new SpaceShooterGame(frame).start(100);
		frame.setVisible(true);
	}
	
	public SpaceShooterGame(JFrame frame) {
		super(frame);
	}
	
	@Override
	public void createScene() {
		View view = new View();
		
		view.setFov(45.0f);
		view.setNear(1);
		view.setFar(1000);
		registerView("front", view);
		view.translate(0, 0, -10);
		
		GroupNode root = getRoot();
		
		OBJModel spaceshipModel = OBJModelLoader.load(renderer, "data/spaceship/viper.obj", false);
		
		LeafNode spaceshipNode = new LeafNode();
		spaceshipNode.scale(0.5f, 0.5f, 0.5f);
		spaceshipNode.attachSceneObject(spaceshipModel);
		spaceshipNode.rotateX(-90);
		spaceshipNode.rotateY(90);
		
		root.addChildNode(spaceshipNode);
		
		spaceship = new Spaceship(spaceshipNode);
		
		PointLight pointLight = new PointLight();
		Color4f paleWhite = new Color4f(0.5f, 0.5f, 0.5f, 1.0f);
		pointLight.setComponents(new Color4f[] {
				paleWhite, paleWhite, paleWhite
		});
		pointLight.setAttenuationFactors(0, 0, 0.000025f);
		pointLight.setDirection(new Vector3f(0, 0, 100));
		pointLight.setExponent(0.3f);
		
		LeafNode lightNode = new LeafNode();
		lightNode.attachSceneObject(pointLight);
		lightNode.translate(0, 0, -50);
		
		root.addChildNode(lightNode);
		
		renderer.createSkyBox("data/skybox/up.bmp",
				"data/skybox/left.bmp",
				"data/skybox/down.bmp",
				"data/skybox/right.bmp",
				"data/skybox/front.bmp",
				"data/skybox/back.bmp",
				300);
		
		renderer.addCallback(new RenderingCallback() {
			
			@Override
			public void beforeSceneRendering(Renderer renderer) {
			}
			
			@Override
			public void afterSceneRendering(Renderer renderer) {
				Dimension frameSize = frame.getSize();
				renderer.draw("spaceship stats: ", 40, frameSize.height - 60);
				renderer.draw(" - acceleration: " + spaceship.acceleration, 40, frameSize.height - 90);
				renderer.draw(" - speed: " + spaceship.speed, 40, frameSize.height - 120);
			}
		});
	}
	
	@Override
	public void processInput() {
		if (inputListener.getKey(KeyEvent.VK_ESCAPE)) {
			System.exit(0);
		} else if (inputListener.getKey(KeyEvent.VK_W)) {
			spaceship.accelerate();
		}
		else if (inputListener.getKey(KeyEvent.VK_S)) {
			spaceship.deaccelerate();
		}
		else if (inputListener.getKey(KeyEvent.VK_A)) {
			spaceship.rotateLeft();
		}
		else if (inputListener.getKey(KeyEvent.VK_D)) {
			spaceship.rotateRight();
		}
	}
	
	@Override
	public void update(float deltaTime) {
		spaceship.move(deltaTime);
		spaceship.speedDecay(deltaTime);
	}
	
}
